import { ref,onMounted } from "vue";
import { useRoute } from "vue-router";
import { getCategoryApi } from "@/apis/category";
import { onBeforeRouteUpdate } from "vue-router";


export function useCategory() {
  // 获取分类数据
  const categoryData = ref({});
  const route = useRoute();
  const getCategory = async (id = route.params.id) => {
    const res = await getCategoryApi(id);
    categoryData.value = res.result;
  };
  onMounted(() => {
    getCategory();
  });

  onBeforeRouteUpdate((to) => {
    getCategory(to.params.id);
  });
  return {
    categoryData
  }
}